package petrochina.company.electric.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.hjq.widget.view.DrawableTextView;

import petrochina.company.electric.R;
import petrochina.company.electric.app.AppAdapter;
import petrochina.company.electric.http.api.TaskDetailStepApi;

/**
 * desc : 任务详情数据列表
 */
public final class TaskDetailAdapter extends AppAdapter<TaskDetailStepApi.Bean> {
    private int mUserType;

    public TaskDetailAdapter(Context context, int userType) {
        super(context);
        mUserType = userType;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final DrawableTextView mStatusNumDt;
        private final TextView mTvTaskContent, tvStatusCurrent, tvStatusTarget;
        private final LinearLayout mLlTaskexecute, mLlTaskHelp, mLlStaff;
        private final ConstraintLayout mClManager;


        private ViewHolder() {
            super(R.layout.power_fragment_item);
            mLlTaskexecute = findViewById(R.id.ll_task_execute);
            mLlTaskHelp = findViewById(R.id.ll_task_help);
            mLlStaff = findViewById(R.id.ll_staff);
            mClManager = findViewById(R.id.cl_manager);
            mStatusNumDt = findViewById(R.id.dt_status_num);
            mTvTaskContent = findViewById(R.id.tv_task_content);
            tvStatusCurrent = findViewById(R.id.tv_status_current);
            tvStatusTarget = findViewById(R.id.tv_status_target);
        }

        @Override
        public void onBindView(int position) {
            TaskDetailStepApi.Bean data = getItem(position);
            int status = data.getStatus();
            switch (status) {
                case 3:
                    mStatusNumDt.setLeftDrawable(R.drawable.complete_ic);
                    mLlTaskexecute.setVisibility(View.INVISIBLE);
                    mLlTaskexecute.setClickable(false);
                    mLlTaskHelp.setVisibility(View.INVISIBLE);
                    mLlTaskHelp.setClickable(false);
                    break;
                case 2:
                    mStatusNumDt.setLeftDrawable(R.drawable.unfinished_ic);
                    mLlTaskexecute.setVisibility(View.INVISIBLE);
                    mLlTaskexecute.setClickable(false);
                    mLlTaskHelp.setVisibility(View.INVISIBLE);
                    mLlTaskHelp.setClickable(false);
                    break;
                case 1:
                    mStatusNumDt.setLeftDrawable(R.drawable.underway_ic);
                    mLlTaskexecute.setVisibility(View.VISIBLE);
                    mLlTaskexecute.setClickable(true);
                    mLlTaskHelp.setVisibility(View.VISIBLE);
                    mLlTaskHelp.setClickable(true);
                    break;
                case 0:
                    mStatusNumDt.setLeftDrawable(R.drawable.icon_empty);
                    mLlTaskexecute.setVisibility(View.INVISIBLE);
                    mLlTaskexecute.setClickable(false);
                    mLlTaskHelp.setVisibility(View.INVISIBLE);
                    mLlTaskHelp.setClickable(false);
                    break;
            }
            mLlStaff.setVisibility(mUserType != 1 ? View.VISIBLE : View.GONE);
            mClManager.setVisibility(mUserType == 1 ? View.VISIBLE : View.GONE);
            if (mUserType == 1 && data.getStatus() == 3) {
                tvStatusCurrent.setVisibility(View.VISIBLE);
                tvStatusTarget.setVisibility(View.VISIBLE);
                String state = null;
                switch (data.getEquipType()) {
                    case 0:
                        state = "确定";
                        break;
                    case 1:
                        state = data.getOperType() == 1 ? "关" : "开";
                        break;
                    case 2:
                        state = data.getOperType() == 3 ? "带电" : "不带电";
                        break;
                    case 3:
                        state = data.getActEquipValue() + "欧姆";
                        break;
                    case 4:
                        state = data.getOperType() == 5 ? "上锁" : "去锁";
                        break;
                }
                tvStatusCurrent.setText("实际：" + state);
                String hopeState = data.getEquipType() == 3 ? data.getEquipValue() + "欧姆" : state;
                tvStatusTarget.setText("预期：" + hopeState);
            }else {
                tvStatusCurrent.setVisibility(View.GONE);
                tvStatusTarget.setVisibility(View.GONE);
            }
            mStatusNumDt.setText((position + 1) + "/" + getCount());
            mTvTaskContent.setText(data.getOperInfo());
        }
    }
}